﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Data;
using System.Windows.Data;
using ACC.Extensions.Utilities;
using ACC.Data.Entities.Common;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for HardnessSampleTestWnd.xaml
    /// </summary>
    public partial class HardnessResultWnd : MetroWindow
    {
        public HardnessResultWnd(AccProcess accProcess, List<ClientProcData> listProcessdata)
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
            _accProc = accProcess;
            InitDataGridColumn(listProcessdata);
            this.btnOK.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_OK"), System.Environment.NewLine);
            this.btnNOK.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_NOK"), System.Environment.NewLine);
        }
        private AccProcess _accProc;
        private void InitDataGridColumn(List<ClientProcData> listProcessdata)
        {
            if (listProcessdata == null) return;
            this.dbgDynamic.Columns.Clear();
            foreach (var item in listProcessdata.OrderBy(b => b.Value))
            {
                this.dbgDynamic.Columns.Add(new DataGridTextColumn() 
                {
                    Header = item.Description + System.Environment.NewLine+ item.Name ,
                    Binding = new Binding(item.Name) { Mode = BindingMode.TwoWay },
                    Width = 100
                    
                });
            }
            ObservableCollection<HardnessResult> listHardnessResult = new ObservableCollection<HardnessResult>();
            listHardnessResult.Add(new HardnessResult()
            {
                Hardness1 = 0
                ,Hardness2 = 0
                ,Hardness3 = 0
            });
            this.dbgDynamic.ItemsSource = listHardnessResult;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            HardnessResult hr = (HardnessResult)this.dbgDynamic.Items[0];
            hr.Result ="OK";
            string strBarcode = this.txtBarcode.Text.Trim();
            if(string.IsNullOrEmpty(strBarcode))
            {
                MessageBox.Show("条码不能为空");
                return;
            }

            List<ClientProcData> listProcData = new List<ClientProcData>();
            Type t = hr.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                if(pi!=null)
                {
                    ClientProcData data = new ClientProcData();
                    data.Name = pi.Name;
                    data.Description = pi.Name;
                    data.Value = pi.GetValue(hr, null)==null?"":pi.GetValue(hr, null).ToString();
                    listProcData.Add(data);
                }
                
            }
            ClientResponse accRes = _accProc.Unload(new List<string>() { strBarcode }, listProcData);
            if(accRes.Result== false)
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), accRes.Message);
            }
            else
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "Successfully");
            }

        }

        private void btnNOK_Click(object sender, RoutedEventArgs e)
        {
            HardnessResult hr = (HardnessResult)this.dbgDynamic.Items[0];
            hr.Result = "NOK";

            string strBarcode = this.txtBarcode.Text.Trim();
            if (string.IsNullOrEmpty(strBarcode))
            {
                MessageBox.Show("条码不能为空");
                return;
            }

            List<ClientProcData> listProcData = new List<ClientProcData>();
            Type t = hr.GetType();

            foreach (PropertyInfo pi in t.GetProperties())
            {
                if (pi != null)
                {
                    ClientProcData data = new ClientProcData();
                    data.Name = pi.Name;
                    data.Description = pi.Name;
                    data.Value = pi.GetValue(hr, null) == null ? "" : pi.GetValue(hr, null).ToString();
                    listProcData.Add(data);
                }
            }
            ClientResponse accRes = _accProc.Unload(new List<string>() { strBarcode }, listProcData);
            if (accRes.Result == false)
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), accRes.Message);
            }
            else
            {
                MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "Successfully");
            }

        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
       

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }
    }
}
